package com.terry.androidkotlin.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import common.base.VLog


/**
 * Author: Terry
 * Date: 2020-10-03 22:54
 * Comment:
 */
class AppInstallProgressBar(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private val mBgPaint: Paint
    private var mProgress = 0
    private var mLastProgress = 0
    private val MAX_PROGRESS = 200

    init {
        mBgPaint = Paint()
        mBgPaint.style = Paint.Style.FILL
        mBgPaint.isAntiAlias = true
        mBgPaint.color = Color.parseColor("#7FE0E0E0")
    }

    override fun onDraw(canvas: Canvas) {
        val rectf = RectF()
        rectf.left = 0F
        rectf.top = 0F
        rectf.right = width.toFloat()
        rectf.bottom = height.toFloat()
        canvas.drawArc(rectf, 0f, 360F * mProgress.toFloat() / MAX_PROGRESS, true, mBgPaint)
        canvas.save();
        canvas.clipRect(0, 0, 300, 300);
        val paint = Paint()
        paint.color = Color.RED
        paint.style = Paint.Style.FILL
        canvas.drawRect(Rect(100, 100, width - 100, height - 100), paint)
        canvas.restore();
    }

    fun start() {
        val anim = ValueAnimator.ofInt(0, MAX_PROGRESS)
        anim.duration = 3000L
        anim.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator?) {
                val progress = animation!!.animatedValue.toString().toInt()
                mProgress = progress
                if (mProgress == mLastProgress) {
                    return
                }
                VLog.i("progress", progress.toString())
                mLastProgress = progress
                postInvalidate()
            }
        })
        anim.start()
    }
}